package com.klangner.lc;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Enumeration;
import java.util.Vector;

public class TestRunner {

	private static final String TESTS_DIR = "./test-data";
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		TestRunner	runner;
		int	testCount = 0;
		int	errorCount = 0;
		
		// Find all tests
		File testDataPath = new File(TESTS_DIR);
		
		File[]	testFiles = testDataPath.listFiles();
		
		for(File testPath : testFiles){
			if(testPath.isDirectory() && !testPath.isHidden()){
				runner = new TestRunner();
				testCount ++;
				if( !runner.executeTest(testPath) )
					errorCount ++;
			}
		}
		
		System.out.println("Found: " + errorCount + " errors.");
	}


	/**
	 * Execute single test
	 * @param testPath
	 * @return true if test passed
	 */
	private boolean executeTest(File testPath) {
		
		Vector<Page>	srcPages = new Vector<Page>();
		Page					expectedPage;
		Page					compiledPage;
		
		// read input files
		File[]	files = testPath.listFiles(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String name) {
				return (name.startsWith("page") && name.endsWith(".xml"));
			}
		});
		
		for(File file : files){
			Page page = new Page();
			page.load(file);
			srcPages.add(page);
		}

		// read destination file
		File file = new File(testPath.getPath() + "/expected.xml");
		expectedPage = new Page();
		expectedPage.load(file);
		
		// compile
		compiledPage = compile(srcPages.elements());
		
		// check
		compiledPage.setWidth(expectedPage.getWidth());
		if( !compiledPage.equals(expectedPage) ){
			System.out.println("Error in file: " + testPath.getName());
//			System.out.println("expected: ");
//			System.out.println(expectedPage);
//			System.out.println("received: ");
//			System.out.println(compiledPage);
//			System.out.println("");
			return false;
		}
		
		return true;
	}


	/**
	 * Compile page
	 * @param pages
	 * @return
	 */
	private static Page compile(Enumeration<Page> pages) {

		return pages.nextElement();
	}

}
